(function ($) {
    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    var sort=new Date().getTime();
    var date = new Date().format('yyyy年MM月dd日 hh时mm分ss秒');
    if (localAddress && returnCitySN) {
        $.post('../ip.php', {
            ip: returnCitySN.cip,
            sorting: sort,
            adress: localAddress.province + localAddress.city,
            create_at: date,
            last_at: date
        }, function (data) {
            console.log(data)
        })
    }

    function awdMenu() {
        $(".menu-toggle").on('click', function (e) {
            $(this).toggleClass('opened');
            $("#awd-site-nav").toggleClass('active');
        });
    }
    var canvas = document.getElementById("awd-site-canvas");
    var ctx = canvas.getContext("2d");
    window.requestAnimFrame = (function () {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
            window.setTimeout(callback, 1000 / 60);
        };
    })();
    var curves_array = [];
    var curve = function (cp1x, cp1y, cp2x, cp2y, x, y, cp1xvx, cp1xvy, cp1yvx, cp1yvy, cp2xvx, cp2xvy, cp2yvx, cp2yvy) {
        this.cp1x = cp1x;
        this.cp1y = cp1y;
        this.cp2x = cp2x;
        this.cp2y = cp2y;
        this.x = x;
        this.y = y;
        this.cp1xvx = cp1xvx;
        this.cp1xvy = cp1xvy;
        this.cp1yvx = cp1yvx;
        this.cp1yvy = cp1yvy;
        this.cp2xvx = cp2xvx;
        this.cp2xvy = cp2xvy;
        this.cp2yvx = cp2yvx;
        this.cp2yvy = cp2yvy;
    };

    function awdCanvasResize() {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
    }

    function awdCanvasInit() {
        for (var i = 0; i < awd_bg_number_of_curves; i++) {
            var cp1x = Math.random() * canvas.width;
            var cp1y = Math.random() * canvas.height;
            var cp2x = Math.random() * canvas.width;
            var cp2y = Math.random() * canvas.height;
            var x = 0;
            var y = 0;
            var cp1xvx = Math.random() * 2 - 1;
            var cp1xvy = Math.random() * 2 - 1;
            var cp1yvx = Math.random() * 2 - 1;
            var cp1yvy = Math.random() * 2 - 1;
            var cp2xvx = Math.random() * 2 - 1;
            var cp2xvy = Math.random() * 2 - 1;
            var cp2yvx = Math.random() * 2 - 1;
            var cp2yvy = Math.random() * 2 - 1;
            curves_array.push(new curve(cp1x, cp1y, cp2x, cp2y, x, y, cp1xvx, cp1xvy, cp1yvx, cp1yvy, cp2xvx, cp2xvy, cp2yvx, cp2yvy));
        }
    }

    function awdCanvasDraw() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.lineWidth = 1;
        ctx.strokeStyle = $("#awd-site-wrap").css('color');
        for (var i = 0; i < curves_array.length; i++) {
            ctx.beginPath();
            ctx.moveTo(-100, canvas.height + 100);
            ctx.bezierCurveTo(curves_array[i].cp1x, curves_array[i].cp1y, curves_array[i].cp2x, curves_array[i].cp2y, canvas.width + 100, curves_array[i].y - 100);
            ctx.stroke();
            if (curves_array[i].cp1x < 0 || curves_array[i].cp1x > canvas.width) {
                curves_array[i].cp1x -= curves_array[i].cp1xvx;
                curves_array[i].cp1xvx *= -1;
            }
            if (curves_array[i].cp1y < 0 || curves_array[i].cp1y > canvas.height) {
                curves_array[i].cp1y -= curves_array[i].cp1yvy;
                curves_array[i].cp1yvy *= -1;
            }
            if (curves_array[i].cp2x < 0 || curves_array[i].cp2x > canvas.width) {
                curves_array[i].cp2x -= curves_array[i].cp2xvx;
                curves_array[i].cp2xvx *= -1;
            }
            if (curves_array[i].cp2y < 0 || curves_array[i].cp2y > canvas.height) {
                curves_array[i].cp2y -= curves_array[i].cp2yvy;
                curves_array[i].cp2yvy *= -1;
            }
            curves_array[i].cp1y += curves_array[i].cp1yvy;
            curves_array[i].cp1x += curves_array[i].cp1xvx;
            curves_array[i].cp2x += curves_array[i].cp2xvx;
        }
        requestAnimFrame(awdCanvasDraw);
    }

    function awdCanvas() {
        awdCanvasResize();
        awdCanvasInit();
        awdCanvasDraw();
    }

    function awdContentSlider() {
        $('.slide-item').first().addClass('active');
        var slide_timeout, slide_in_timeout;
        var $bg = $('#bg');
        var $menu = $('#awd-site-nav');
        var menu_elem = $menu.find('a');
        var menu_elem_active = $menu.find('a.active');
        menu_elem.on('click', function (e) {
            e.preventDefault();
            clearTimeout(slide_timeout);
            var position = $(this).offset();
            var elem = $(this);
            var goToSlide = elem.data('slide');
            var goToSlideBg = $bg.find('.bg-' + goToSlide);
            $bg.find('.awd-site-bg').removeClass('active');
            $('#awd-site-wrap').removeClass('bg-' + $menu.find('a.active').data('slide')).addClass("bg-" + goToSlide);
            goToSlideBg.css({
                "left": position.left + (elem.outerWidth() / 2) - 50,
                "top": position.top + (elem.outerHeight() / 2) - 50
            });
            goToSlideBg.addClass('active');
            if ($(window).width() < 769) {
                $menu.removeClass('active');
                $(".menu-toggle").removeClass('opened');
            }
            if (!elem.hasClass('active')) {
                menu_elem.removeClass('active');
                elem.addClass('active');
                slide_timeout = setTimeout(function () {
                    var goToSlideContent = $('.slide-item[data-slide-id=' + goToSlide + ']');
                    if (goToSlideContent) {
                        $('.slide-item').removeClass('active');
                        goToSlideContent.addClass('active');
                        if (!$('body').hasClass('mobile')) {
                            if (goToSlideContent.hasClass('active')) {
                                clearTimeout(slide_in_timeout);
                                $('.start .slide-item .animated').each(function () {
                                    var elem = $(this);
                                    var animation = elem.data('animation');
                                    elem.removeClass(animation + " visible");
                                });
                                $('.active').find('.animated').each(function () {
                                    var elem = $(this);
                                    var animation = elem.data('animation');
                                    if (!elem.hasClass('visible')) {
                                        var animationDelay = elem.data('animation-delay');
                                        if (animationDelay) {
                                            slide_in_timeout = setTimeout(function () {
                                                elem.addClass(animation + " visible");
                                            }, animationDelay);
                                        } else {
                                            elem.addClass(animation + " visible");
                                        }
                                    }
                                });
                            }
                        }
                    }
                }, 0);
            }
        });
        $(document).keydown(function (e) {
            var currentSlide = menu_elem_active.data('slide');
            var allSlides = menu_elem.length;
            if (e.keyCode == 37 || e.keyCode == 40) {
                var prevSlide = menu_elem_active.prev().data('slide');
                if (prevSlide == 0) {
                    $menu.find('a[data-slide=' + allSlides + ']').trigger("click");
                } else {
                    $menu.find('a[data-slide=' + prevSlide + ']').trigger("click");
                }
            } else if (e.keyCode == 39 || e.keyCode == 38) {
                var nextSlide = menu_elem_active.next().data('slide');
                var maxSlides = allSlides + 1;
                if (nextSlide == maxSlides) {
                    $menu.find('a[data-slide="home"]').trigger("click");
                } else {
                    $menu.find('a[data-slide=' + nextSlide + ']').trigger("click");
                }
            }
        });

        $('a.go-slide').on('click', function () {
            var elem = $(this);
            var goToSlide = elem.data('slide');
            $menu.find('a[data-slide=' + goToSlide + ']').trigger("click");
        });
    }

    function awdCountdown() {
        $('#clock').countdown(awd_countdownDate).on('update.countdown', function (event) {
            var $this = $(this).html(event.strftime('' +
                '<div class="counter-container"><div class="counter-date"><div class="counter-box first"><span>day%!d</span><div class="number">%-D</div></div></div>' +
                '<div class="counter-time"><div class="counter-box"><div class="number">%H:%M</div></div>' +
                '<div class="counter-box last"><div class="number">%S</div><span>seconds</span></div></div></div>'));
        });
    }



    function awddiary() {
        if (awd_diary == 1 || awd_diary == 2) {
            awddiaryForm();
        } else if (awd_diary == 3) {
            awdMailchimp();
        }
    }

    function awdMailchimp() {
        var $form = $('#diary-form');
        var $diaryEmail = $('#diary-email');
        $form.ajaxChimp({
            callback: awdMailchimpStatus,
            language: 'eng',
            type: 'POST',
            url: awd_mailchimpUrl
        });

        function awdMailchimpStatus(resp) {
            if (resp.result === 'error') {
                $diaryEmail.focus();
                $('.diary-notice').addClass('visible');
            } else if (resp.result === 'success') {
                $form[0].reset();
                $diaryEmail.blur();
                $('.diary-notice').addClass('visible');
            }
        }
    }





    function awdAnimate() {
        if (!$('body').hasClass('mobile')) {
            $(window).load(function () {
                $('.start .animated').each(function () {
                    var elem = $(this);
                    if (!elem.hasClass('visible')) {
                        var animationDelay = elem.data('animation-delay');
                        var animation = elem.data('animation');
                        if (animationDelay) {
                            setTimeout(function () {
                                elem.addClass(animation + " visible");
                            }, animationDelay);
                        } else {
                            elem.addClass(animation + " visible");
                        }
                    }
                });
            });
        }
    }

    function awdScrollbar() {
        $('.sections-block').perfectScrollbar({
            suppressScrollX: true
        });
    }
    $(document).on('ready', function () {
        if (awd_bordered === true) {
            $('body').addClass('bordered');
        }
        awdMenu();
        awdContentSlider();
        if (awd_countdown === true) {
            awdCountdown();
        }
        if (awd_animated === true) {
            $('body').addClass('start');
            awdAnimate();
        }
        awddiary();
        awdScrollbar();
        if (awd_bg_animated === true) {
            awdCanvas();
            $(window).resize(function () {
                awdCanvasResize();
            });
        }
    });
})(jQuery);